var form;
var table;
var layer;
var laydate;
layui.use(['layer','form','table','laydate'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;
    laydate = layui.laydate;
    //加载按钮 课程订单信息
    selUserType(2);
    load();
});
//按钮点击事件
function load(){
    $(".tit_refresh").bind('click',function(){
        location.reload();
    })
}
//获取课程订单的数量赋值给按钮
function selCourseOrderCount(userType) {
    $.ajax({
        url: "/SyOperationController/selCommentCount",
        dataType: "json",
        type:"post",
        data:{"type":3,"userType":userType},
        success: function (data) {
            $("#btn1").text("全部评论("+data.data[0].alls+")");
            $("#btn2").text("正常("+data.data[0].through+")");
            $("#btn3").text("已拦截("+data.data[0].intercept+")");
            $("#btn4").text("已删除("+data.data[0].remove+")");
        }
    })
}
//统计历史/今天数量
historyTodayCount()
function historyTodayCount() {
    $.ajax({
        url: "/SyOperationController/historyTodayCount",
        dataType: "json",
        type:"post",
        data:{"type":3},
        success: function (data) {
            $("#historyCount").text(data.data.historyCount);
            $("#historyNumber").text(data.data.historyNumber);
            $("#todayCount").text(data.data.todayCount);
            $("#todayNumber").text(data.data.todayNumber);
        }
    })
}
function selUserType(userType){
    if(userType==2){
        $("#btn5").css({"color":"#FFFFFF","background":"#009688"});
        $("#btn6").css({"color":"#555555","background":"#FFFFFF"});
        $("input[name=userType]").val(2);
        selComment(0);
        selCourseOrderCount(2);
    }else if(userType==3){
        $("#btn5").css({"color":"#555555","background":"#FFFFFF"});
        $("#btn6").css({"color":"#FFFFFF","background":"#009688"});
        $("input[name=userType]").val(3);
        selComment(0);
        selCourseOrderCount(3)
    }else{
        $("#btn5").css({"color":"#FFFFFF","background":"#009688"});
        $("#btn6").css({"color":"#555555","background":"#FFFFFF"});
        $("input[name=userType]").val(2);
        selComment(0);
        selCourseOrderCount(2);
    }
}
//点击按钮，课程订单信息
function selComment(num) {
    var userType = $("input[name=userType]").val();
    if(num==1){
        $("#btn2").css({"color":"#FFFFFF","background":"#009688"});
        $("#btn1").css({"color":"#555555","background":"#FFFFFF"});
        $("#btn3").css({"color":"#555555","background":"#FFFFFF"});
        $("#btn4").css({"color":"#555555","background":"#FFFFFF"});
        IfBeck(num,userType);
    }else if(num==2){
        $("#btn3").css({"color":"#FFFFFF","background":"#009688"});
        $("#btn1").css({"color":"#555555","background":"#FFFFFF"});
        $("#btn2").css({"color":"#555555","background":"#FFFFFF"});
        $("#btn4").css({"color":"#555555","background":"#FFFFFF"});
        IfBeck(num,userType);
    }else if(num==3){
        $("#btn4").css({"color":"#FFFFFF","background":"#009688"});
        $("#btn1").css({"color":"#555555","background":"#FFFFFF"})
        $("#btn2").css({"color":"#555555","background":"#FFFFFF"})
        $("#btn3").css({"color":"#555555","background":"#FFFFFF"})
        IfBeck(num,userType);
    }else if(num==0){
        $("#btn1").css({"color":"#FFFFFF","background":"#009688"});
        $("#btn2").css({"color":"#555555","background":"#FFFFFF"})
        $("#btn3").css({"color":"#555555","background":"#FFFFFF"})
        $("#btn4").css({"color":"#555555","background":"#FFFFFF"})
        IfBeck(num,userType);
    }
}
//判断前后端
function IfBeck(num,userType){
    if(userType==2){
        getAjax(num,userType);
    }else if(userType==3){
        getBackAjax(num,userType);
    }
}
//ajax获取参数
function getAjax(status,userType) {
    $.ajax({
        url: "/SyOperationController/selEssayComment",
        type: "post",
        dataType: "json",
        data:{"type":3,"status":status,"userType":userType},
        success: function (data) {
            getTable(data.data);
        }
    });
}
//获取后端评论数据
function getBackAjax(status,userType){
    $.ajax({
        url: "/SyOperationController/selEssayBackComment",
        type: "post",
        dataType: "json",
        data:{"type":3,"status":status,"userType":userType},
        success: function (data) {
            getTable(data.data);
        }
    });
}
//课程订单数据列表
function getTable(data){
    var tb1=table.render({
        elem:"#table_id",
        title:"评论列表",
        limit:10,
        data:data,
        page:true,
        cols:[[
            {type:'checkbox',width:60},
            {title:'序号',align:'center',width:80,templet:function(data){
                    return data.LAY_INDEX;
            }},
            {field:'account',title:'成员账号',align:'center'},
            {field:'nickname',title:'昵称',align:'center'},
            {field:'course_name',title:'所属文章',align:'center'},
            {field:'comment_date',title:'发言时间',align:'center',templet:function(d){
                    var date=settime(d.comment_date);
                    return date;
                }},
            {field:'content',title:'发言内容',align:'center'},
            {field:'status',title:'状态',align:'center',templet:function(d){
                    if(d.status==1){
                        return  "正常"
                    }else if(d.status==2){
                        return "已拦截"
                    }else if(d.status==3){
                        return "已删除"
                    }else{
                        return "异常"
                    }
                }},
            {field:'',title:'操作',align:'center',templet:function(d){
                    if(d.status==1){
                        return "<span><a onclick='javascript:selCommentInfo(\""+d.commodity_id+"\")' style='color:#1ABC9C;cursor:pointer;'>查看</a></span>"+
                            "<span style='margin-left: 10px'><a onclick='javascript:updComment(\""+d.id+"\",\""+d.status+"\",\""+d.content+"\")' style='color:#1ABC9C;cursor:pointer;'>拦截</a></span>"+
                            "<span style='margin-left: 10px'><a  onclick='javascript:delComment(\""+d.id+"\",\""+d.content+"\")' style='color:#1ABC9C;cursor:pointer;'>删除</a></span>"
                    }else if(d.status==2){
                        return "<span><a onclick='javascript:selCommentInfo(\""+d.commodity_id+"\")' style='color:#1ABC9C;cursor:pointer;'>查看</a></span>"+
                            "<span style='margin-left: 10px'><a onclick='javascript:updComment(\""+d.id+"\",\""+d.status+"\",\""+d.content+"\")' style='color:#1ABC9C;cursor:pointer;'>恢复</a></span>"+
                            "<span style='margin-left: 10px'><a onclick='javascript:delComment(\""+d.id+"\",\""+d.content+"\")' style='color:#1ABC9C;cursor:pointer;'>删除</a></span>"
                    }else if(d.status==3){
                        return "<span style='color:#1ABC9C;'><a onclick='javascript:clearComment(\""+d.id+"\")' style='color:#1ABC9C;cursor:pointer;'>清理</a></span>"
                    }else{
                        return "异常";
                    }
            }}
        ]]
    });
}
//查看所属评论课程
function selCommentInfo(commodityId){
    var index = layer.open({
        type:2,
        title:"文章详情",
        area:['800px','600px'],
        shade:0.4,
        offset:'auto',
        maxmin: true,
        content:"/operation/comment/TeaEssayView.html",
        success:function(layero,index) {
            //查找当前引入的iframe内容
            var obj = layero.find('iframe').contents();
            //根据选择器选择input,并设置值
            obj.find('input[name=courseId]').val(commodityId);
        }
    });
    layer.full(index);
}
//评论状态修改
function updComment(id,st,content) {
    var status="";
    if(st==1){
        status=2;
        layer.confirm("您确定要拦截`"+content+"`信息吗?",function (rel) {
            setStatus(id,status);
        },function (res) {
        });
    }else if(st==2){
        status=1;
        layer.confirm("您确定要恢复`"+content+"`信息吗?",function (rel) {
            setStatus(id,status);
        },function (res) {
        });
    }
}
function setStatus(id,status){
    $.ajax({
        url: "/SyOperationController/updComment",
        type: "post",
        dataType: "json",
        data:{"id":id,"status":status},
        success: function (data) {
            if(data.code==0){
                layer.msg("成功",{icon:1});
                selUserType(2);
            }else{
                layer.msg("失败",{icon:2});
            }
        }
    });
}
//评论状态修改为删除
function delComment(id,content) {
    layer.confirm("您确定要删除`"+content+"`信息吗?",function (rel) {
        $.ajax({
            url: "/SyOperationController/updComment",
            type: "post",
            dataType: "json",
            data:{"id":id,"status":3},
            success: function (data) {
                if(data.code==0){
                    layer.msg("删除成功",{icon:1});
                    selUserType(2);
                }else{
                    layer.msg("删除失败",{icon:2});
                }
            }
        });
    },function (res) {
    });
}
//彻底清理数据
function clearComment(id){
    $.ajax({
        url: "/SyOperationController/clearComment",
        type: "post",
        dataType: "json",
        data:{"id":id},
        success: function (data) {
            if(data.code==0){
                layer.msg("已彻底删除");
                selUserType(2);
            }else{
                layer.msg("删除失败");
            }
        }
    });
}
/*日期转换*/
function settime(da){
    var date=new Date(da);
    var year=date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    var hours=date.getHours();
    var mm=date.getMinutes();
    var s=date.getSeconds();
    return year+'-'+month+'-'+day+' '+hours+':'+mm+':'+s;
};